Partial Class pages_ProductTypes_ProductType : Inherits CPage

#Region "Querystring"
    Public ReadOnly Property TypeId() As Byte
        Get
            Return CWeb.RequestByte("typeId")
        End Get
    End Property
    Public ReadOnly Property IsEdit() As Boolean
        Get
            Return TypeId <> Integer.MinValue
        End Get
    End Property
#End Region

#Region "Members"
    Private m_productType As CProductType
#End Region

#Region "Data"
    Public ReadOnly Property [ProductType]() As CProductType
        Get
            If IsNothing(m_productType) Then
                If IsEdit Then
                    m_productType = CProductType.Cache.GetById(TypeId)
                    If IsNothing(m_productType) Then CSitemap.RecordNotFound("ProductType", TypeId)
                Else
                    m_productType = New CProductType
                    'Inserts: set parentId here (if applicable)
                End If
            End If
            Return m_productType
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.ProductTypeEdit(Me.ProductType.TypeId))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.ProductTypes)
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        With ddTypeParentTypeId
            .DataTextField = "TypeName"
            .DataValueField = "TypeId"
            .DataSource = CProductType.Cache
            .DataBind()
            .BlankItem("-- Select ParentType --", 0)
            .Required = ProductType.TypeParentIsRequired
        End With

        ddTypes.DataSource = CProductType.Cache
        ddTypes.DataBind()
        CDropdown.BlankItem(ddTypes, "-- New Type --", 0)
        CDropdown.SetValue(ddTypes, TypeId)

        'Page Title
        If IsEdit Then            
            Me.Title = "ProductType Details"
        Else
            Me.Title = "Create New ProductType"
        End If
                
        'Textbox logic
        'Me.Form.DefaultFocus = txtProductTypeName.Textbox.ClientID;
        Me.Form.DefaultButton = btnSave.UniqueID   'txtProductTypeName.OnReturnPress(btnSave)

        'Button Text
        btnDelete.Visible = IsEdit
        If IsEdit Then btnCancel.Text = "Back" Else btnSave.Text = "Create ProductType"
        If IsEdit Then AddButton(CSitemap.ProductTypeAdd(), "Create a new ProductType")
    End Sub
    Protected Overrides Sub PageLoad()
        If Page.IsPostback Then Exit Sub

        LoadProductType()
    End Sub
#End Region

#Region "Event Handlers - Form"
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Not Me.IsValid() Then Exit Sub
        SaveProductType()
        Refresh()
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ReturnToList()
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Me.ProductType.Delete()
            'CCache.ClearCache()
        ReturnToList()        
    End Sub
#End Region

#Region "Private - Load/Save"
    Protected Sub LoadProductType()
        With Me.ProductType
            txtTypeName.Text = .TypeName
            ddTypeParentTypeId.ValueInt = .TypeParentTypeId
            chkTypeParentIsRequired.Checked = .TypeParentIsRequired
            chkTypeIsFree.Checked = .TypeIsFree
            chkTypeCanHaveAudio.Checked = .TypeCanHaveAudio
            chkTypeCanHavePdfOrWord.Checked = .TypeCanHavePdfOrWord
            chkTypeCanHaveHtmlNotesOrPreview.Checked = .TypeCanHaveHtmlNotesOrPreview
            chkTypeCanHaveVimeo.Checked = .TypeCanHaveVimeo
            chkTypeCanHaveTranscript.Checked = .TypeCanHaveTranscript
            txtTypeDescription.Text = .TypeDescription
        End With
    End Sub
    Protected Sub SaveProductType()
        With Me.ProductType
            .TypeName = txtTypeName.Text
            .TypeParentTypeId = ddTypeParentTypeId.ValueInt
            .TypeParentIsRequired = chkTypeParentIsRequired.Checked
            .TypeIsFree = chkTypeIsFree.Checked
            .TypeCanHaveAudio = chkTypeCanHaveAudio.Checked
            .TypeCanHavePdfOrWord = chkTypeCanHavePdfOrWord.Checked
            .TypeCanHaveHtmlNotesOrPreview = chkTypeCanHaveHtmlNotesOrPreview.Checked
            .TypeCanHaveVimeo = chkTypeCanHaveVimeo.Checked
            .TypeCanHaveTranscript = chkTypeCanHaveTranscript.Checked
            .TypeDescription = txtTypeDescription.Text

            .Save()
        End With
            'CCache.ClearCache()
    End Sub
#End Region

    Protected Sub ddTypes_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddTypes.SelectedIndexChanged
        Response.Redirect(CSitemap.ProductTypeEdit(CDropdown.GetByte(ddTypes)))
    End Sub
End Class
